<?php
if(!defined('DCS_ROOT')) {exit('Access Denied');}

class IndexModel {
	var $DB;
	function IndexModel ( $DB ){
		$this->DB = $DB;
	}

	function getPlayMovie($order,$number){
		$time = time();
		$sql = "SELECT * FROM movie WHERE playtime <= '$time' AND endtime >= '$time' ORDER BY playtime LIMIT 0,$number";
		$this->DB->query($sql);
		return $this->DB->fetchAll();	
	}

	function getTopic($order,$start,$numbers){
		$sql = "SELECT g.domain,g.name,p.pid,p.posttime,p.username,p.title,p.hits,p.replys,m.nickname FROM groups AS g,group_post AS p,member AS m WHERE g.id = p.gid AND p.username = m.username ORDER BY $order DESC LIMIT $start,$numbers";
		$this->DB->query($sql);
		return $this->DB->fetchAll();		
	}
	
	function getComments($order,$number){
		$sql = "SELECT c.id,c.uid,c.mid,c.rating,c.contents,c.rt,c.comments,c.weibo_id,m.username,m.avatars,m.profile_image_url,m2.id,m2.title mtitle 
		FROM movie_comments AS c,member AS m,movie AS m2 
		WHERE c.uid = m.uid AND c.mid = m2.id AND c.contents != '' ORDER BY c.$order DESC LIMIT 0,$number ";
		$this->DB->query($sql);
		return $this->DB->fetchAll();	
	}

	function getReviews($order,$number){
		$sql = "SELECT r.id,r.uid,r.mid,r.rating,r.title,r.contents,r.reply,r.addtime,m.username,m.avatars,m.profile_image_url,m2.title as mtitle 
		FROM movie_review AS r,member AS m,movie AS m2 
		WHERE r.uid = m.uid AND r.mid = m2.id ORDER BY $order DESC LIMIT 0,$number ";
		$this->DB->query($sql);
		return $this->DB->fetchAll();	
	}

	function getHotComments($num){
		$sql = "SELECT uid,username,avatars FROM member WHERE uid IN($uids) ORDER BY $order DESC LIMIT 0,$number ";
		$this->DB->query($sql);
		return $this->DB->fetchAll();
	}

	function getFriendCollect($fids,$number,$type){		
		//todo  AND m.endtime >= '$time' 
		$monthtime = strtotime(date('Y-m-d',time()))  - 86400 * 30 ;
		$time = time();
		$sql = "SELECT c.mid,c.uid,m.title,u.username,u.avatars,u.profile_image_url FROM movie_comments AS c, movie AS m,member AS u WHERE 
		c.uid IN($fids) AND c.addtime >= '$monthtime' AND c.mid = m.id AND c.type = '$type' AND c.uid = u.uid
		ORDER BY c.addtime DESC LIMIT 0,$number ";
		$this->DB->query($sql);
		$members = $this->DB->fetchAll();
		//var_dump($members);

		foreach($members as $k => $v){
			$movies[$v['uid']]['uid'] = $v['uid'];
			$movies[$v['uid']]['username'] = $v['username'];
			$movies[$v['uid']]['avatars'] = $v['avatars'];
			$movies[$v['uid']]['profile_image_url'] = $v['profile_image_url'];
			if(empty($movies[$v['uid']]['movie'])){
				$movies[$v['uid']]['movie'] .=  '<a href="/movie/'.$v['mid'].'/">《'.$v['title'].'》</a>';
			}else{
				$movies[$v['uid']]['movie'] .=  '、<a href="/movie/'.$v['mid'].'/">《'.$v['title'].'》</a>';
			}
		}
		return $movies;
		//todo
		// 取多数
		//$temp = multi_array_sort($members, 'addtime',SORT_DESC);
		//$uids = array_slice($temp,0,8);
		//return arrayUnique($members,'uid');
	}

	function getHotMovie($ids,$order){
		$sql = "SELECT * FROM movie WHERE id IN ($ids) ORDER BY $order ";
		$this->DB->query($sql);
		return $this->DB->fetchAll();	
	}
	
	//取朋友短评
	function getFriendComments($fids,$order,$number){
		$sql = "SELECT c.id,c.uid,c.rating,c.contents,c.support,c.addtime,c.rt,c.comments,c.weibo_id,c.mid,m.username,m.avatars,m.profile_image_url FROM movie_comments AS c, member AS m WHERE c.uid IN($fids) AND c.uid = m.uid AND c.contents != '' ORDER BY $order DESC LIMIT 0,$number ";
		$this->DB->query($sql);
		return $this->DB->fetchAll();	
	}

	function getComment($mid){
		$sql = "SELECT c.id,c.mid,c.uid,c.contents,m.username,m.avatars,m.profile_image_url
		FROM movie_comments AS c,member AS m 
		WHERE c.uid = m.uid AND c.mid = '$mid' AND c.contents != '' ORDER BY c.addtime DESC ";
		$this->DB->query($sql);
		return $this->DB->fetchRow();	
	}



}
?>
